//
//  UpdateController.m
//  D&D Manager
//
//  Created by Matthew Hutton on 26/07/2007.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "UpdateController.h"
#import "SpellMethods.h"
#import "AbilityChanged.h"
#import "NSManagedObjectExtended.h"

@implementation UpdateController
-(id)initWithBattleStore:(NSManagedObject *)bs
 withCharacterController:(NSArrayController *)cc
		  withMainWindow:(NSWindow *)window{
	[super init];
	//NSLog(@"hello updating");
	[NSBundle loadNibNamed:@"UpdateStuff"
					 owner:self];
	battleStore=bs;
	characterController=cc;
	mainScreen=window;
	mObjC=[battleStore managedObjectContext];
	model=[[mObjC persistentStoreCoordinator] managedObjectModel];
	return self;
}
-(IBAction)cancelUpdate:(id)sender{
	NSObject *obj=[mainScreen delegate];
	[mainScreen setDelegate:nil];
	[mainUpdateWindowOnePointFour orderOut:self];
	[NSApp endSheet:mainUpdateWindowOnePointFour];
	[mainScreen setDelegate:obj];
	[[[NSDocumentController sharedDocumentController] currentDocument] close];
}
-(IBAction)continueWithBackup:(id)sender{
	NSPersistentStoreCoordinator *store=[mObjC persistentStoreCoordinator];
	NSURL *oldURL=[store URLForPersistentStore:[[store persistentStores] objectAtIndex:0]];
	NSString *fileName=[oldURL path];
	if([fileName hasSuffix:@".campaign"]){
		//process fileName to add a \\ before " s in the string.
		NSRange range;
		range.length=1;
		int i=0;
		//NSLog(@"morning");
		while(i<[fileName length]){
			range.location=i;
			NSString *specificChar=[fileName substringWithRange:range];
			if([specificChar isEqualToString:@"\""]){
				fileName=[NSString stringWithFormat:@"%@\\\\\\%@",[fileName substringToIndex:i],[fileName substringFromIndex:i]];
				//to skip 'next' character which is this one again.
				i=i+3;
			}
			i++;
		}
		//NSLog(@"complete");
		range=[fileName rangeOfString:@".campaign" options:NSBackwardsSearch];
		NSString *fileNameBase=[fileName substringToIndex:range.location];
		NSString *fileName2;
		//NSString *fileNameBASE=;
		NSFileManager *fileManager=[NSFileManager defaultManager];
		if([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@ backup.campaign",fileNameBase]]){
			BOOL goOn=NO;
			int j=2;
			while(!goOn){
				if([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@ #%d backup.campaign",fileNameBase,j]]){
					j++;
				}else{
					fileName2=[NSString stringWithFormat:@"%@ #%d backup.campaign",fileNameBase,j];
				}
			}
		}else{
			fileName2=[NSString stringWithFormat:@"%@ backup.campaign",fileNameBase];
		}
	}else{
		[mObjC save:nil];
	}
	/*[self convertAbilityBonusSpellsToWorkWithVersionOnePointFourWithExistingTransferred:[transferABS state]];
	[mainUpdateWindow orderOut:self];
	[NSApp endSheet:mainUpdateWindow];
	if(needsCustomCharToDisplay){
		if(ccController==nil){
			ccController=[[CharacterClassController alloc] init];
		}
		[NSApp beginSheet:[ccController ccWindow]
		   modalForWindow:mainScreen
			modalDelegate:self
		   didEndSelector:nil
			  contextInfo:NULL];
	}*/
	[self versionOnePointFourBasicProcessing];
}
-(IBAction)continueWithoutBackup:(id)sender{
	NSPersistentStoreCoordinator *store=[mObjC persistentStoreCoordinator];
	NSURL *oldURL=[store URLForPersistentStore:[[store persistentStores] objectAtIndex:0]];
	NSString *fileName=[oldURL absoluteString];
	if([fileName hasSuffix:@".quest"]){
		NSRange range=[fileName rangeOfString:@".quest" options:NSBackwardsSearch];
		fileName=[NSString stringWithFormat:@"%@.campaign",[fileName substringToIndex:range.location]];
		NSURL *newURL=[NSURL URLWithString:fileName];
		NSError *error=nil;
		[store migratePersistentStore:[store persistentStoreForURL:oldURL] toURL:newURL options:nil withType:NSXMLStoreType error:&error];
		if(error!=nil){
			NSLog(@"migration error: %@",error);
			return;
		}
		NSPersistentDocument *doc=[[NSDocumentController sharedDocumentController] currentDocument];
		[doc setFileURL:newURL];
		[mObjC save:nil];
		NSPersistentStoreCoordinator *store=[mObjC persistentStoreCoordinator];
		NSString *fileName=[[store URLForPersistentStore:[[store persistentStores] objectAtIndex:0]] path];
		range=[fileName rangeOfString:@".campaign" options:NSBackwardsSearch];
		//cut off extension, add .quest and loose localhost stuff from the beginning.
		fileName=[NSString stringWithFormat:@"%@.quest",[fileName substringToIndex:range.location]];
		//process fileName to add a \\ before " s in the string.
		range.length=1;
		int i=0;
		while(i<[fileName length]){
			range.location=i;
			NSString *specificChar=[fileName substringWithRange:range];
			if([specificChar isEqualToString:@"\""]){
				fileName=[NSString stringWithFormat:@"%@\\%@",[fileName substringToIndex:i],[fileName substringFromIndex:i]];
				//to skip 'next' character which is this one again.
				i=i+1;
			}
			i++;
		}
		NSAppleScript *script=[[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"set mPath to POSIX file \"%@\"\n tell application \"Finder\" to delete mPath",fileName]];
		NSDictionary *dict=nil;
		[script executeAndReturnError:&dict];
		if(dict!=nil){
			NSLog(@"Applescript Error:%@",dict);
		}		
	}
	//[self convertAbilityBonusSpellsToWorkWithVersionOnePointFourWithExistingTransferred:[transferABS state]];
	[mainUpdateWindowOnePointFour orderOut:self];
	[NSApp endSheet:mainUpdateWindowOnePointFour];
	/*if(needsCustomCharToDisplay){
		if(ccController==nil){
			ccController=[[CharacterClassController alloc] init];
		}
		[NSApp beginSheet:[ccController ccWindow]
		   modalForWindow:mainScreen
			modalDelegate:self
		   didEndSelector:nil
			  contextInfo:NULL];
	}*/	
	[self versionOnePointFourBasicProcessing];
}
-(void)versionOnePointFourBasicProcessing{
	NSArray *characterClasses=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"characterClass" substitutionVariables:[NSDictionary dictionary]] error:nil];
	int i;
	for(i=0;i<[characterClasses count];i++){
		NSManagedObject *characterClass=[characterClasses objectAtIndex:i];
		//set new defaults.
		if([characterClass valueForKey:@"keyAttribute"]==nil){
			[characterClass setValue:@"None"
							  forKey:@"keyAttribute"];
		}
		if([characterClass valueForKey:@"turnsUndead"]==nil){
			[characterClass setValue:[NSNumber numberWithBool:NO]
							  forKey:@"turnsUndead"];
		}
		if([characterClass valueForKey:@"turnsAsLevelMinusThree"]==nil){
			[characterClass setValue:[NSNumber numberWithBool:NO]
							  forKey:@"turnsAsLevelMinusThree"];
		}
		NSString *name=[characterClass valueForKey:@"name"];
		if(![[characterClass valueForKey:@"castsSpells"] boolValue]){
			if(![name isEqualToString:@"Barbarian"] && ![name isEqualToString:@"Fighter"] && ![name isEqualToString:@"Monk"] && ![name isEqualToString:@"Rogue"]){
				//for turning
				needsCustomCharToDisplay=YES;
			}
			continue;
		}
		//OK check the name
		if([name isEqualToString:@"Wizard"]){
			[characterClass setValue:@"Intelligence"
							  forKey:@"keyAttribute"];
		}else if([name isEqualToString:@"Sorcerer"] || [name isEqualToString:@"Bard"]){
			[characterClass setValue:@"Charisma"
							  forKey:@"keyAttribute"];
		}else if([name isEqualToString:@"Cleric"] || [name isEqualToString:@"Druid"] || [name isEqualToString:@"Paladin"] || [name isEqualToString:@"Ranger"]){
			[characterClass setValue:@"Wisdom"
							  forKey:@"keyAttribute"];
			if([name isEqualToString:@"Cleric"]){
				[characterClass setValue:[NSNumber numberWithBool:YES]
								  forKey:@"turnsUndead"];
			}
			if([name isEqualToString:@"Paladin"]){
				[characterClass setValue:[NSNumber numberWithBool:YES]
								  forKey:@"turnsUndead"];
				[characterClass setValue:[NSNumber numberWithBool:YES]
								  forKey:@"turnsAsLevelMinusThree"];
			}
		}else{
			//other class must be edited manually.
			needsCustomCharToDisplay=YES;
		}
	}
	NSArray *characters=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"character" substitutionVariables:[NSDictionary dictionary]] error:nil];
	for(i=0;i<[characters count];i++){
		NSManagedObject *obj=[characters objectAtIndex:i];
		NSSet *spellLevels=[obj valueForKey:@"spellLevel"];
		if([spellLevels count]==0){
			continue;
		}
		int j;
		for(j=1;j<4;j++){
			NSManagedObject *characterClass=[obj valueForKey:[NSString stringWithFormat:@"characterClass%d",j]];
			if(characterClass==nil){
				continue;
			}
			if(![[characterClass valueForKey:@"castsSpells"] boolValue]){
				continue;
			}
			int casterLevel=[[obj valueForKey:[NSString stringWithFormat:@"charClassLvl%d",j]] intValue];
			NSMutableSet *set=[NSMutableSet setWithCapacity:5];
			[set setSet:[characterClass valueForKey:@"spellLevel"]];
			[set intersectSet:spellLevels];
			NSArray *array=[set allObjects];
			int k;
			for(k=0;k<[array count];k++){
				[SpellMethods setupBaseAvailableSpellsForCC:characterClass
											  forSpellLevel:[array objectAtIndex:k]
											  atCasterLevel:casterLevel];
			}
		}
	}
	
	/*array=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"character" substitutionVariables:[NSDictionary dictionary]] error:nil];
	NSArray *superskills=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"superskill" substitutionVariables:[NSDictionary dictionary]] error:nil];
	NSArray *superskillNames=[superskills valueForKey:@"name"];
	//NSLog(@"array.count=%d",[array count]);
	for(i=0;i<[array count];i++){
		NSManagedObject *obj=[array objectAtIndex:i];
		BOOL weSetToNil=YES;
		int j;
		for(j=1;j<4;j++){
			if([obj valueForKey:[NSString stringWithFormat:@"characterClass%d",j]]!=nil){
				if([[[obj valueForKey:[NSString stringWithFormat:@"characterClass%d",j]] valueForKey:@"castsDomainSpells"] boolValue]){
					weSetToNil=NO;
					break;
				}
			}
		}
		if(weSetToNil){
			[obj setValue:@"Choose..."
				   forKey:@"domain1"];
			[obj setValue:@"Choose..."
				   forKey:@"domain2"];
			[obj setValue:@"Choose..."
				   forKey:@"domain3"];
		}
		//check and link up skills.
		NSArray *skills=[[obj valueForKey:@"skills"] allObjects];
		for(j=0;j<[skills count];j++){
			NSManagedObject *skill=[skills objectAtIndex:i];
			int k;
			BOOL foundOne=NO;
			for(k=0;k<[superskillNames count];k++){
				if([[superskillNames objectAtIndex:j] isEqualToString:[skill valueForKey:@"name"]]){
					foundOne=YES;
					NSManagedObject *superskill=[superskills objectAtIndex:j];
					//now link them together, and tie the skills properties to the superskills ones.
					[skill setValue:superskill
							 forKey:@"superskill"];
					[skill setValue:[superskill valueForKey:@"ability"]
							 forKey:@"ability"];
					[AbilityChanged setupSkillAbilityBonusForSkill:skill
													  forCharacter:obj];
					//process armourCheck
					[AbilityChanged processArmourCheckPenaltyForSkill:skill
														 forCharacter:obj
																wasOn:[[skill valueForKey:@"armourCheckApplies"] boolValue]
																 isOn:[[superskill valueForKey:@"armourCheckApplies"] boolValue]];
					if([[superskill valueForKey:@"multipleSubskills"] boolValue]){
						[skill setValue:[NSNumber numberWithBool:YES]
								 forKey:@"nameEditable"];
					}else{
						[skill setValue:[NSNumber numberWithBool:NO]
								 forKey:@"nameEditable"];
					}
					break;
				}
			}
			if(!foundOne){
				//create new superskill.
				NSManagedObject *superskill=[[NSManagedObject alloc] initWithEntity:[[model entitiesByName]  objectForKey:@"Superskill"] insertIntoManagedObjectContext:mObjC];
				[skill setValue:superskill
						 forKey:@"superskill"];
				[superskill setValue:[skill valueForKey:@"ability"]
							  forKey:@"ability"];
				[superskill setValue:[skill valueForKey:@"armourCheckApplies"]
							  forKey:@"armourCheckApplies"];
			}
		}
	}*/
	[continueWithBackupButton setEnabled:YES];
	[continueWithoutBackupButton setEnabled:YES];
}
-(void)preVersionOnePointTwoStuff{
	//NSLog(@"setting up spells.");
	//setup spells.
	NSArray *spells=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"NewSpell" substitutionVariables:[NSDictionary dictionary]]
										 error:nil];
	int i;
	for(i=0;i<[spells count];i++){
		NSManagedObject *spell=[spells objectAtIndex:i];
		[SpellMethods processRangeDescriptionForSpell:spell];
		//setup description and summoning possibilities.		
		NSMutableString *mutStr=[[NSMutableString alloc] initWithCapacity:10];
		NSData *data=[spell valueForKey:@"theDescription"];
		if([data length]>0){
			NSString *str=[[NSString alloc] initWithData:data encoding:1];
			NSRange range;
			range.location=[str rangeOfString:@"\\f0\\fs24 \\cf0 " options:NSBackwardsSearch].location;
			if(range.location!=NSNotFound){
				range.location=range.location+14;
				range.length=[str length]-range.location-1;
				if(range.length>0 && range.location<[str length]){
					NSString *str2=[str substringWithRange:range];
					if([str2 length]>0){
						[mutStr appendString:str2];
					}
				}
			}
			[spell setValue:nil
					 forKey:@"theDescription"];
			[str release];
		}
		data=[spell valueForKey:@"summoningPossibilities"];
		if([data length]>0){
			//NSText *text=[[NSText alloc] init];
			//[text selectAll:self];
			NSString *str=[[NSString alloc] initWithData:data encoding:1];
			NSRange range;
			range.location=[str rangeOfString:@"\\f0\\fs24 \\cf0 " options:NSBackwardsSearch].location+14;
			range.length=[str length]-range.location-15;
			NSString *str2=[str substringWithRange:range];
			if([str2 length]>0){
				if([mutStr length]>0){
					[mutStr appendString:@"\n\n"];
				}
				[mutStr appendString:str2];
			}
			[str release];
			[spell setValue:nil
					 forKey:@"summoningPossibilities"];
		}
		if([mutStr length]>0){
			[spell setValue:mutStr
					 forKey:@"theDescription2"];
		}
		[mutStr release];
	}
	//now process characters.
	NSArray *chars=[characterController arrangedObjects];
	int iss[[chars count]];
	for(i=0;i<[chars count];i++){
		NSManagedObject *obj=[chars objectAtIndex:i];
		//check for duplicate template number.
		int oldID=[[obj valueForKey:@"id"] intValue];
		int j;
		if([[obj valueForKey:@"temp"] boolValue]){
			if([obj valueForKey:@"id"]!=nil){
				NSArray *array=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"templateCharacter" substitutionVariables:[NSDictionary dictionaryWithObject:[obj valueForKey:@"id"] forKey:@"idValue"]]
													error:nil];
				if([array count]==1){
					NSManagedObject *template=[array objectAtIndex:0];
					[obj setValue:template
						   forKey:@"templateObject"];
				}
			}
			[obj setValue:[battleStore valueForKey:@"currentTempUID"]
				   forKey:@"id"];
			[battleStore setValue:[NSNumber numberWithInt:[[battleStore valueForKey:@"currentTempUID"] intValue]-1]
						   forKey:@"currentTempUID"];	
		}else{
			for(j=0;j<i;j++){
				if(iss[j]==oldID){
					[obj setValue:[battleStore valueForKey:@"currentUID"]
						   forKey:@"id"];
					[battleStore setValue:[NSNumber numberWithInt:[[battleStore valueForKey:@"currentUID"] intValue]+1]
								   forKey:@"currentUID"];							
					break;
				}
			}
		}
		//sets up cleric level property.
		[AbilityChanged setLevelOrXPForChar:obj
						  forCharacterClass:nil
						  bonusLimitedToL20:[[battleStore valueForKey:@"limitConBonus"] boolValue]];
		//move portrait to own object to improve performance.
		NSManagedObject *portrait=[[NSManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"Portrait" inManagedObjectContext:mObjC] insertIntoManagedObjectContext:mObjC];
		[portrait setValue:obj
					forKey:@"character"];
		NSData *data=[obj valueForKey:@"portrait"];
		if(data!=nil){
			[portrait setValue:data
						forKey:@"portraitData"];
		}
		[obj setValue:nil
			   forKey:@"portrait"];
		//set up spells.
		for(j=1;j<4;j++){
			NSString *theClass=[obj valueForKey:[NSString stringWithFormat:@"charClassName%d",j]];
			if([theClass isEqualToString:@"Barbarian"] || [theClass isEqualToString:@"Fighter"] || [theClass isEqualToString:@"Monk"] || [theClass isEqualToString:@"Rogue"]){
				continue;
			}
			if([theClass isEqualToString:@"Sorcerer"]){
				theClass=@"Wizard";
			}
			NSArray *spellsTemp=[[[obj valueForKey:@"spells"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@==1",[theClass lowercaseString]]]];
			[obj setValue:[NSSet setWithArray:spellsTemp]
				   forKey:[NSString stringWithFormat:@"spells%d",j]];
		}
		[obj setValue:nil
			   forKey:@"spells"];
	}
}
-(void)preVersionOnePointThreeStuff{
	if([[battleStore valueForKey:@"fileVersion"] intValue]<62){
		NSArray *chars=[characterController arrangedObjects];
		int i;
		for(i=0;i<[chars count];i++){
			NSManagedObject *obj=[chars objectAtIndex:i];
			NSArray *feats=[[obj valueForKey:@"feats"] allObjects];
			int j;
			for(j=0;j<[feats count];j++){
				NSManagedObject *feat=[feats objectAtIndex:j];
				if([[feat valueForKey:@"type"] intValue]!=3){
					[feat setValue:nil
							forKey:@"miscUse"];
				}
			}
		}
	}
	if([[battleStore valueForKey:@"fileVersion"] intValue]<63){
		NSArray *spells=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"NewSpell" substitutionVariables:[NSDictionary dictionary]]
											 error:nil];
		int i;
		for(i=0;i<[spells count];i++){
			NSManagedObject *spell=[spells objectAtIndex:i];
			if([spell valueForKey:@"saveType"]==nil && [[spell valueForKey:@"saveEffect"] intValue]>0){
				[spell setValue:[NSNumber numberWithInt:0]
						 forKey:@"saveType"];
			}
		}
	}
	if([[battleStore valueForKey:@"fileVersion"] intValue]<64){
		NSArray *chars=[characterController arrangedObjects];
		int i;
		for(i=0;i<[chars count];i++){
			NSManagedObject *obj=[chars objectAtIndex:i];
			if([obj valueForKey:@"portrait"]!=nil){
				NSManagedObject *portrait=[[NSManagedObject alloc] initWithEntity:[[model entitiesByName]  objectForKey:@"Portrait"]
												   insertIntoManagedObjectContext:mObjC];
				[portrait setValue:[obj valueForKey:@"portrait"]
							forKey:@"portraitData"];
				[obj setValue:portrait
					   forKey:@"portraitObject"];
				[obj setValue:nil
					   forKey:@"portrait"];
			}
			//now sort skills and weapons to remove boo boo about setup of weapons.
			if([[[[obj valueForKey:@"weapons"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name==\"Unarmed Attack\""]] count]>=1){
				[[[[[obj valueForKey:@"weapons"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name==\"Unarmed Attack\""]] objectAtIndex:0] setValue:[NSNumber numberWithBool:NO] forKey:@"canRemove"];
			}else{
				NSManagedObject *weap=[[NSManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"Weapon" inManagedObjectContext:mObjC]
											   insertIntoManagedObjectContext:mObjC];
				[weap setValue:@"Unarmed Attack"
						forKey:@"name"];
				[weap setValue:[NSNumber numberWithBool:NO]
						forKey:@"twoHandedWeapon"];
				[weap setValue:[NSNumber numberWithBool:NO]
						forKey:@"canRemove"];
				[weap setValue:@"Bludgeoning"
						forKey:@"damageType"];
				[weap setValue:[NSNumber numberWithInt:3]
						forKey:@"sizeOfDamageDie"];
				[weap setValue:obj
						forKey:@"character"];
				[obj setValue:weap
					   forKey:@"defaultWeapon"];
				[obj setValue:weap
					   forKey:@"defaultWeapon"];
				[obj setValue:@"Unarmed Attack"
					   forKey:@"defaultWeaponForDisp"];
				[weap setValue:[NSNumber numberWithBool:YES]
						forKey:@"defaultWeapon"];						
			}
			if([[[[obj valueForKey:@"skills"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name==\"Concentration\""]] count]==0){
				NSManagedObject *skill=[[NSManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"Skill" inManagedObjectContext:mObjC]
												insertIntoManagedObjectContext:mObjC];
				[skill setValue:@"Concentration"
						 forKey:@"name"];
				[skill setValue:[NSNumber numberWithBool:NO]
						 forKey:@"canRemove"];
				[skill setValue:[NSNumber numberWithInt:4]
						 forKey:@"ability"];
				[skill setValue:obj
						 forKey:@"character"];
			}else{
				NSManagedObject *skill=[[[[obj valueForKey:@"skills"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name==\"Concentration\""]] objectAtIndex:0];
				[skill setValue:[NSNumber numberWithBool:NO]
						 forKey:@"canRemove"];
				[skill setValue:[NSNumber numberWithInt:4]
						 forKey:@"ability"];
			}
			if([[[[obj valueForKey:@"skills"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name==\"Listen\""]] count]==0){
				NSManagedObject *skill=[[NSManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"Skill" inManagedObjectContext:mObjC]
												insertIntoManagedObjectContext:mObjC];
				[skill setValue:@"Listen"
						 forKey:@"name"];
				[skill setValue:[NSNumber numberWithBool:NO]
						 forKey:@"canRemove"];
				[skill setValue:[NSNumber numberWithInt:3]
						 forKey:@"ability"];
				[skill setValue:obj
						 forKey:@"character"];
			}else{
				NSManagedObject *skill=[[[[obj valueForKey:@"skills"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name==\"Listen\""]] objectAtIndex:0];
				[skill setValue:[NSNumber numberWithBool:NO]
						 forKey:@"canRemove"];
				[skill setValue:[NSNumber numberWithInt:3]
						 forKey:@"ability"];
			}
			if([[[[obj valueForKey:@"skills"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name==\"Search\""]] count]==0){
				NSManagedObject *skill=[[NSManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"Skill" inManagedObjectContext:mObjC]
												insertIntoManagedObjectContext:mObjC];
				[skill setValue:@"Search"
						 forKey:@"name"];
				[skill setValue:[NSNumber numberWithBool:NO]
						 forKey:@"canRemove"];
				[skill setValue:[NSNumber numberWithInt:1]
						 forKey:@"ability"];
				[skill setValue:obj
						 forKey:@"character"];
			}else{
				NSManagedObject *skill=[[[[obj valueForKey:@"skills"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name==\"Search\""]] objectAtIndex:0];
				[skill setValue:[NSNumber numberWithBool:NO]
						 forKey:@"canRemove"];
				[skill setValue:[NSNumber numberWithInt:1]
						 forKey:@"ability"];
				
			}
			if([[[[obj valueForKey:@"skills"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name==\"Spot\""]] count]==0){
				NSManagedObject *skill=[[NSManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"Skill" inManagedObjectContext:mObjC]
												insertIntoManagedObjectContext:mObjC];
				[skill setValue:@"Spot"
						 forKey:@"name"];
				[skill setValue:[NSNumber numberWithBool:NO]
						 forKey:@"canRemove"];
				[skill setValue:[NSNumber numberWithInt:3]
						 forKey:@"ability"];
				[skill setValue:obj
						 forKey:@"character"];
			}else{
				NSManagedObject *skill=[[[[obj valueForKey:@"skills"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name==\"Spot\""]] objectAtIndex:0];
				[skill setValue:[NSNumber numberWithBool:NO]
						 forKey:@"canRemove"];
				[skill setValue:[NSNumber numberWithInt:3]
						 forKey:@"ability"];
			}
			//now check object has an id.
			if([obj valueForKey:@"id"]==nil){
				[obj setValue:[battleStore valueForKey:@"currentUID"]
					   forKey:@"id"];
				[battleStore setValue:[NSNumber numberWithInt:[[battleStore valueForKey:@"currentUID"] intValue]+1]
							   forKey:@"currentUID"];
			}
		}
	}
	//NSLog(@"processing 80 code");
	//now process 80 code.
	NSArray *spells=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"NewSpell" substitutionVariables:[NSDictionary dictionary]]
										 error:nil];
	NSArray *array=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"characterClass" substitutionVariables:[NSDictionary dictionary]]
										error:nil];
	NSManagedObject *bard, *cleric, *druid, *paladin, *ranger, *sorcerer, *wizard;
	int i;
	for(i=0;i<[array count];i++){
		NSManagedObject *theClass=[array objectAtIndex:i];
		NSString *name=[theClass valueForKey:@"name"];
		if([name isEqualToString:@"Bard"]){
			bard=theClass;
		}else if([name isEqualToString:@"Cleric"]){
			cleric=theClass;
		}else if([name isEqualToString:@"Druid"]){
			druid=theClass;
		}else if([name isEqualToString:@"Paladin"]){
			paladin=theClass;
		}else if([name isEqualToString:@"Ranger"]){
			ranger=theClass;
		}else if([name isEqualToString:@"Sorcerer"]){
			sorcerer=theClass;
		}else if([name isEqualToString:@"Wizard"]){
			wizard=theClass;
		}
	}
	for(i=0;i<[spells count];i++){
		NSManagedObject *spell=[spells objectAtIndex:i];
		NSMutableSet *charClasses=[NSMutableSet setWithCapacity:4];
		if([[spell valueForKey:@"bard"] boolValue]){
			NSManagedObject *spellClass=[SpellMethods setupSpellClassForCharClass:bard];
			[spellClass setValue:[spell valueForKey:@"levelBard"]
						  forKey:@"level"];
			[charClasses addObject:spellClass];
		}
		if([[spell valueForKey:@"cleric"] boolValue]){
			NSManagedObject *spellClass=[SpellMethods setupSpellClassForCharClass:cleric];
			[spellClass setValue:[spell valueForKey:@"levelCleric"]
						  forKey:@"level"];
			[charClasses addObject:spellClass];
		}
		if([[spell valueForKey:@"druid"] boolValue]){
			NSManagedObject *spellClass=[SpellMethods setupSpellClassForCharClass:druid];
			[spellClass setValue:[spell valueForKey:@"levelDruid"]
						  forKey:@"level"];
			[charClasses addObject:spellClass];
		}
		if([[spell valueForKey:@"paladin"] boolValue]){
			NSManagedObject *spellClass=[SpellMethods setupSpellClassForCharClass:paladin];
			[spellClass setValue:[spell valueForKey:@"levelPaladin"]
						  forKey:@"level"];
			[charClasses addObject:spellClass];
		}
		if([[spell valueForKey:@"ranger"] boolValue]){
			NSManagedObject *spellClass=[SpellMethods setupSpellClassForCharClass:ranger];
			[spellClass setValue:[spell valueForKey:@"levelRanger"]
						  forKey:@"level"];
			[charClasses addObject:spellClass];
		}
		if([[spell valueForKey:@"wizard"] boolValue]){
			if(1){
				NSManagedObject *spellClass=[SpellMethods setupSpellClassForCharClass:wizard];
				[spellClass setValue:[spell valueForKey:@"levelWizard"]
							  forKey:@"level"];
				[charClasses addObject:spellClass];
			}
			if(1){
				NSManagedObject *spellClass=[SpellMethods setupSpellClassForCharClass:sorcerer];
				[spellClass setValue:[spell valueForKey:@"levelWizard"]
							  forKey:@"level"];
				[charClasses addObject:spellClass];
			}
		}
		if([[spell valueForKey:@"domain"] boolValue]){
			//domains
			NSString *str=[spell valueForKey:@"domainName"];
			NSManagedObject *domainClass=[[[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"domainClass" substitutionVariables:[NSDictionary dictionary]] error:nil] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name==\"%@\"",str]]] objectAtIndex:0];
			NSManagedObject *spellClass=[SpellMethods setupSpellClassForCharClass:domainClass];
			[spellClass setValue:[spell valueForKey:@"levelDomain"]
						  forKey:@"level"];
			[charClasses addObject:spellClass];
			//[charClasses addObject
							}
		[spell setValue:charClasses
				 forKey:@"spellClass"];
		//now check feat filter.
		if([[spell valueForKey:@"featFilter"] isEqualToString:@""] || [spell valueForKey:@"featFilter"]==nil){
			[spell setValue:@"All"
					 forKey:@"featFilter"];
		}
		if(![[spell valueForKey:@"featFilter"] isEqualToString:@"All"]){
			if([[spell valueForKey:@"spellAffects"] intValue]==0){//as the property didn't used to exist.
				[spell setValue:[NSNumber numberWithInt:2]
						 forKey:@"spellAffects"];
			}
		}
		//check whether it's a vs undead spell
		if([[spell valueForKey:@"damageType"] isEqualToString:@"vs Undead"]){
			[spell setValue:nil
					 forKey:@"damageType"];
			[spell setValue:[NSNumber numberWithInt:2]
					 forKey:@"spellAffects"];
			[spell setValue:@"Undead"
					 forKey:@"featFilter"];
		}
		//now check spell type
		int oldType=[[spell valueForKey:@"type"] intValue];
		if(oldType==0){
			[spell setValue:@"Healing"
					 forKey:@"subtype"];
		}
		if(oldType==2 || oldType==3 || oldType==5){
			[spell setValue:[NSNumber numberWithInt:2]
					 forKey:@"type"];
			if(oldType==5){
				[spell setValue:@"Feat"
						 forKey:@"subtype"];
				[SpellMethods processFeatDescriptionForSpell:spell];
				//NSLog(@"feat Description=%@",[spell valueForKey:@"featDescription"]);
			}else{
				if(oldType==3){//paralysis spell.
					[spell setValue:[NSNumber numberWithBool:YES]
							 forKey:@"paralysesTargets"];
				}
				[spell setValue:@"Damage"
						 forKey:@"subtype"];
			}
		}else if(oldType==4){
			[spell setValue:[NSNumber numberWithInt:3]
					 forKey:@"type"];
			[spell setValue:@"Neither"
					 forKey:@"subtype"];
		}else if(oldType==6){
			[spell setValue:[NSNumber numberWithInt:4]
					 forKey:@"type"];
			[spell setValue:@"Neither"
					 forKey:@"subtype"];
		}
		//set up area of effect.
		if([[spell valueForKey:@"areaOfEffect"] intValue]==0){
			[spell setValue:[NSNumber numberWithBool:NO]
					 forKey:@"areaOfEffectOn"];
		}else{
			[spell setValue:[NSNumber numberWithInt:[[spell valueForKey:@"areaOfEffect"] intValue]-1]
					 forKey:@"areaOfEffect"];
			[spell setValue:[NSNumber numberWithBool:YES]
					 forKey:@"areaOfEffectOn"];
		}
		//clear old stuff.
		[spell setValue:nil
				 forKey:@"bard"];
		[spell setValue:nil
				 forKey:@"levelBard"];
		[spell setValue:nil
				 forKey:@"cleric"];
		[spell setValue:nil
				 forKey:@"levelCleric"];
		[spell setValue:nil
				 forKey:@"domain"];
		[spell setValue:nil
				 forKey:@"levelDomain"];
		[spell setValue:nil
				 forKey:@"druid"];
		[spell setValue:nil
				 forKey:@"levelDruid"];
		[spell setValue:nil
				 forKey:@"paladin"];
		[spell setValue:nil
				 forKey:@"levelPaladin"];
		[spell setValue:nil
				 forKey:@"ranger"];
		[spell setValue:nil
				 forKey:@"levelRanger"];
		[spell setValue:nil
				 forKey:@"wizard"];
		[spell setValue:nil
				 forKey:@"levelWizard"];
						}
	NSArray *charArray=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"character" substitutionVariables:[NSDictionary dictionary]]
											error:nil];
	NSMutableArray *classArray=[NSMutableArray arrayWithCapacity:10];
	[classArray setArray:[NSArray arrayWithObjects:@"Barbarian", @"Bard", @"Cleric", @"Druid", @"Fighter", @"Monk", @"Paladin", @"Ranger",@"Rogue", @"Sorcerer", @"Wizard", nil]];
	for(i=0;i<[charArray count];i++){
		NSManagedObject *obj=[charArray objectAtIndex:i];
		int j;
		//check classes aren't duplicated.
		NSString *class1=[obj valueForKey:@"charClassName1"];
		NSString *class2=[obj valueForKey:@"charClassName2"];
		NSString *class3=[obj valueForKey:@"charClassName3"];
		if([class1 isEqualToString:class2] && ![class1 isEqualToString:@"None"]){
			[obj setValue:@"Choose..."
				   forKey:@"charClassName2"];
			[obj setValue:nil
				   forKey:@"characterClass2"];
		}
		if([class1 isEqualToString:class3] && ![class1 isEqualToString:@"None"]){
			[obj setValue:@"Choose..."
				   forKey:@"charClassName3"];
			[obj setValue:nil
				   forKey:@"characterClass3"];
		}
		if([class2 isEqualToString:class3] && ![class2 isEqualToString:@"None"]){
			[obj setValue:@"Choose..."
				   forKey:@"charClassName3"];
			[obj setValue:nil
				   forKey:@"characterClass3"];
		}
		//link up to character class objects.
		for(j=1;j<4;j++){
			if([[obj valueForKey:[NSString stringWithFormat:@"charClassName%d",j]] isEqualToString:@"None"]){
				[obj setValue:@"Choose..."
					   forKey:[NSString stringWithFormat:@"charClassName%d",j]];
			}else{
				NSString *str=[obj valueForKey:[NSString stringWithFormat:@"charClassName%d",j]];
				if([str isEqualToString:@"Sourcerer"]){
					str=@"Sorcerer";
					[obj setValue:@"Sorcerer"
						   forKey:[NSString stringWithFormat:@"charClassName%d",j]];
				}
				int k;
				BOOL defaultClass=NO;
				for(k=0;k<[classArray count];k++){
					if([str isEqualToString:[classArray objectAtIndex:k]]){
						defaultClass=YES;
						break;
					}
				}
				if(!defaultClass){
					[classArray addObject:str];
					/*if(ccController==nil){
						ccController=[[CharacterClassController alloc] init];
					}*/
					//[ccController addNewCharacterClassWithName:str];
					needsCustomCharToDisplay=YES;
				}
				NSManagedObject *characterClass=[[[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"characterClass" substitutionVariables:[NSDictionary dictionaryWithObject:str forKey:@"value"]] error:nil] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name LIKE[c] \"%@\"",str]]] objectAtIndex:0];
				//NSLog(@"characterClass=%@",characterClass);
				[obj setValue:characterClass
					   forKey:[NSString stringWithFormat:@"characterClass%d",j]];
			}
		}
		//change spell level objects.
		NSManagedObject *charClass1=[obj valueForKey:@"characterClass1"];
		NSManagedObject *charClass2=[obj valueForKey:@"characterClass2"];
		NSManagedObject *charClass3=[obj valueForKey:@"characterClass3"];
		NSArray *spellLevels=[[obj valueForKey:@"spellLevel"] allObjects];
		int i;
		for(i=0;i<[spellLevels count];i++){
			NSManagedObject *spellLevel=[spellLevels objectAtIndex:i];
			NSManagedObject *characterClass;
			if([[spellLevel valueForKey:@"charClass"] isEqualToString:class1]){
				characterClass=charClass1;
				[spellLevel setValue:charClass1
							  forKey:@"characterClass"];
			}else if([[spellLevel valueForKey:@"charClass"] isEqualToString:class2]){
				characterClass=charClass2;
				[spellLevel setValue:charClass2
							  forKey:@"characterClass"];
			}else{
				characterClass=charClass3;
				[spellLevel setValue:charClass3
							  forKey:@"characterClass"];
			}
			[spellLevel setValue:nil
						  forKey:@"charClass"];
			if([[characterClass valueForKey:@"allSpontaneous"] boolValue]){
				continue;
			}
			int j;
			NSMutableSet *spellClassesForCC=[NSMutableSet setWithCapacity:5];
			//NSLog(@"characterClass.name=%@, spellClasses (CC)=%d",[characterClass valueForKey:@"name"],[spellClassesForCC count]);
			NSArray *memorisedSpells=[[spellLevel valueForKey:@"memorisedSpell"] allObjects];
			for(j=0;j<[memorisedSpells count];j++){
				//check if it's a domain spell or not.
				NSManagedObject *memorisedSpell=[memorisedSpells objectAtIndex:j];
				NSManagedObject *spellClass;
				if([[memorisedSpell valueForKey:@"domain"] boolValue]){
					NSManagedObject *spell=[memorisedSpell valueForKey:@"spell"];
					spellClass=[[[[spell valueForKey:@"spellClass"] allObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"domainSpell==1"]] objectAtIndex:0];
				}else{
					[spellClassesForCC setSet:[characterClass valueForKey:@"spellClass"]];
					NSManagedObject *spell=[memorisedSpell valueForKey:@"spell"];
					NSSet *spellClasses=[spell valueForKey:@"spellClass"];
					//NSLog(@"spell.name=%@, spellClasses.count=%d",[spell valueForKey:@"name"],[spellClasses count]);
					[spellClassesForCC intersectSet:spellClasses];
					//NSLog(@"spellClassesForCC.count=%d",[spellClassesForCC count]);
					spellClass=[spellClassesForCC anyObject];
				}
				[memorisedSpell setValue:spellClass
								  forKey:@"spellClass"];
				[memorisedSpell setValue:nil
								  forKey:@"spell"];
			}
		}
		//processing spells.
		for(i=1;i<4;i++){
			NSManagedObject *characterClass=[obj valueForKey:[NSString stringWithFormat:@"characterClass%d",i]];
			if(characterClass==nil){
				continue;
			}
			if(![[characterClass valueForKey:@"castsSpells"] boolValue]){
				continue;
			}
			NSMutableSet *spellClasses1=[NSMutableSet setWithCapacity:5];
			[spellClasses1 setSet:[characterClass valueForKey:@"spellClass"]];
			//check if characterClass casts domain spells.
			if([[characterClass valueForKey:@"castsDomainSpells"] boolValue]){
				NSError *error=nil;
				NSArray *domainClasses=[[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"domainClass" substitutionVariables:[NSDictionary dictionary]] error:&error] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name like[c] \"%@\" OR name like[c] \"%@\" OR name like[c] \"%@\"",[obj valueForKey:@"domain1"],[obj valueForKey:@"domain2"],[obj valueForKey:@"domain3"]]]];
				if(error!=nil){
					NSLog(@"error=%@",error);
				}
				int j;
				for(j=0;j<[domainClasses count];j++){
					[spellClasses1 unionSet:[[domainClasses objectAtIndex:j] valueForKey:@"spellClass"]];
				}
			}
			NSArray *spells=[[obj valueForKey:[NSString stringWithFormat:@"spells%d",i]] allObjects];
			int j;
			NSMutableSet *spellClasses2=[NSMutableSet setWithCapacity:10];
			for(j=0;j<[spells count];j++){
				[spellClasses2 unionSet:[[spells objectAtIndex:j] valueForKey:@"spellClass"]];
			}
			[spellClasses2 intersectSet:spellClasses1];
			[spellClasses2 unionSet:[obj valueForKey:@"spellClass"]];
			[obj setValue:spellClasses2
				   forKey:@"spellClass"];
			[obj setValue:nil
				   forKey:[NSString stringWithFormat:@"spells%d",i]];
		}
	}
	NSArray *currentSpellArray=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"currentSpell" substitutionVariables:[NSDictionary dictionary]]
													error:nil];
	NSArray *characterClasses=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"characterClass" substitutionVariables:[NSDictionary dictionary]]
												   error:nil];
	for(i=0;i<[currentSpellArray count];i++){
		NSManagedObject *currentSpell=[currentSpellArray objectAtIndex:i];
		NSString *theClassname=[currentSpell valueForKey:@"theClassName"];
		int j;
		for(j=0;j<[characterClasses count];j++){
			if([theClassname isEqualToString:[[characterClasses objectAtIndex:j] valueForKey:@"name"]]){
				NSManagedObject *characterClass=[characterClasses objectAtIndex:j];
				NSSet *spellClasses1=[characterClass valueForKey:@"spellClass"];
				NSMutableSet *spellClasses2=[NSMutableSet setWithCapacity:1];
				[spellClasses2 setSet:[[currentSpell valueForKey:@"spell"] valueForKey:@"spellClass"]];
				[spellClasses2 intersectSet:spellClasses1];
				[currentSpell setValue:[spellClasses2 anyObject]
								forKey:@"spellClass"];
				[currentSpell setValue:characterClass
								forKey:@"characterClass"];
			}
		}
		[currentSpell setValue:nil
						forKey:@"overwrittenSpell"];
		[currentSpell setValue:nil
						forKey:@"theClassName"];
		[currentSpell setValue:nil
						forKey:@"spell"];
	}	
}
-(void)preVersionOnePointFourStuff{
	//NSLog(@"battleStore=%@",battleStore);
	if([[battleStore valueForKey:@"fileVersion"] intValue]<81){
		//NSLog(@"fileVersion<81");
		[battleStore setValue:[NSNumber numberWithBool:YES]
					   forKey:@"showLevelUpWindow"];
		NSArray *array=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"character" substitutionVariables:[NSDictionary dictionary]] error:nil];
		//NSLog(@"array.count=%d",[array count]);
		int j;
		for(j=0;j<[array count];j++){
			//NSLog(@"setting noOfFeats up");
			NSManagedObject *obj=[array objectAtIndex:j];
			[obj setValue:[NSNumber numberWithInt:[AbilityChanged conHPBonusForCharacter:obj
																	   bonusLimitedToL20:[[battleStore valueForKey:@"limitConBonus"] boolValue]]]
				   forKey:@"conBonusHP"];
			[AbilityChanged getNumberOfFeatsForTheCharacter:obj];
		}
	}
	if([[battleStore valueForKey:@"fileVersion"] intValue]<82){
		NSArray *array=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"character" substitutionVariables:[NSDictionary dictionary]] error:nil];
		//NSLog(@"array.count=%d",[array count]);
		int j;
		for(j=0;j<[array count];j++){
			NSManagedObject *obj=[array objectAtIndex:j];
			if(![[obj valueForKey:@"charClassName1"] isEqualToString:@"Choose..."] && [obj valueForKey:@"characterClass1"]==nil){
				NSString *newClass=[obj valueForKey:@"charClassName1"];
				NSArray *array2=[[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"characterClass" substitutionVariables:[NSDictionary dictionary]] error:nil] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name==\"%@\"",newClass]]];
				if([array2 count]==0){
					NSLog(@"class not found");
					continue;
				}
				NSManagedObject *newCC=[array2 objectAtIndex:0];
				[obj setValue:newCC
					   forKey:@"characterClass1"];
			}
			if([obj valueForKey:@"noOfMiscFeats"]==nil){
				[obj setValue:[NSNumber numberWithInt:1]
					   forKey:@"noOfMiscFeats"];
				[obj setValue:[NSNumber numberWithInt:[[obj valueForKey:@"noOfTotalFeats"] intValue]+1]
					   forKey:@"noOfTotalFeats"];
			}
		}
	}
	if([[battleStore valueForKey:@"fileVersion"] intValue]<83){
		NSArray *array=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"character" substitutionVariables:[NSDictionary dictionary]] error:nil];
		int i;
		for(i=0;i<[array count];i++){
			NSManagedObject *obj=[array objectAtIndex:i];
			[obj setValue:nil
				   forKey:@"spells1"];
			[obj setValue:nil
				   forKey:@"spells2"];
			[obj setValue:nil
				   forKey:@"spells3"];
			if(![[battleStore valueForKey:@"continueBattle"] boolValue]){
				[obj setValue:[NSNumber numberWithBool:NO]
					   forKey:@"surprised"];
			}
		}
		//NSLog(@"less than 83");
		NSArray *spellClasses=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"spellClass" substitutionVariables:[NSDictionary dictionary]] error:nil];
		NSMutableArray *newSpellClasses=[NSMutableArray arrayWithCapacity:[spellClasses count]];
		for(i=0;i<[spellClasses count];i++){
			//NSLog(@"i=%d",i);
			NSManagedObject *spellClass=[spellClasses objectAtIndex:i];
			int j;
			BOOL dupeFound=NO;
			for(j=0;j<[newSpellClasses count];j++){
				NSManagedObject *newSpellClass=[newSpellClasses objectAtIndex:j];
				if([newSpellClass isADuplicateOf:spellClass
										mergeToo:YES]){
					dupeFound=YES;
					//NSLog(@"dupeFound");
					//i--;
					break;
				}
			}
			if(!dupeFound){
				[newSpellClasses addObject:spellClass];
			}
		}
		NSArray *spells=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"NewSpell" substitutionVariables:[NSDictionary dictionary]] error:nil];
		NSMutableArray *newSpells=[NSMutableArray arrayWithCapacity:[spells count]];
		for(i=0;i<[spells count];i++){
			//NSLog(@"i=%d",i);
			NSManagedObject *spell=[spells objectAtIndex:i];
			int j;
			BOOL dupeFound=NO;
			for(j=0;j<[newSpells count];j++){
				NSManagedObject *newspell=[newSpells objectAtIndex:j];
				if([newspell isADuplicateOf:spell
								   mergeToo:YES]){
					dupeFound=YES;
					NSLog(@"dupeFound");
					//i--;
					break;
				}
			}
			if(!dupeFound){
				[newSpells addObject:spell];
			}
		}
		NSArray *domains=[mObjC executeFetchRequest:[model fetchRequestFromTemplateWithName:@"domainClass" substitutionVariables:[NSDictionary dictionary]] error:nil];
		NSMutableArray *newDomains=[NSMutableArray arrayWithCapacity:[domains count]];
		for(i=0;i<[domains count];i++){
			//NSLog(@"i=%d",i);
			NSManagedObject *domain=[domains objectAtIndex:i];
			int j;
			BOOL dupeFound=NO;
			for(j=0;j<[newDomains count];j++){
				NSManagedObject *newDomain=[newDomains objectAtIndex:j];
				if([newDomain isADuplicateOf:domain
									mergeToo:YES]){
					dupeFound=YES;
					NSLog(@"dupeFound");
					//i--;
					break;
				}
			}
			if(!dupeFound){
				[newDomains addObject:domain];
			}
		}
	}
}
-(void)runUpdates{
	if([[battleStore valueForKey:@"fileVersion"] intValue]<100){
		[NSApp beginSheet:mainUpdateWindowOnePointFour
		   modalForWindow:mainScreen
			modalDelegate:self
		   didEndSelector:nil
			  contextInfo:NULL];		
		if([[battleStore valueForKey:@"fileVersion"] intValue]<80){
			if([[battleStore valueForKey:@"fileVersion"] intValue]<61){
				[self preVersionOnePointTwoStuff];
			}
			[self preVersionOnePointThreeStuff];
		}
		[self preVersionOnePointFourStuff];
		[self versionOnePointFourBasicProcessing];
	}
}
@end
